//
//  RootTabBarController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/20.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import ESTabBarController_swift

class RootTabBarController: ESTabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addController()
        
    }
    
    //MARK: 添加控制器
    fileprivate func addController() {
        
        self.delegate = self
        
        //账单
        let accountVC = KMNavigationController.init(rootViewController: AccountViewController())
        accountVC.tabBarItem = ESTabBarItem.init(TabBarBgContentView(),title:bill,image:UIImage.init(named: "account_unselect"),selectedImage:UIImage.init(named: "account_select"))
        
        accountVC.tabBarItem.tag = 2
        
        //信用卡
        let creditCardVC = KMNavigationController.init(rootViewController: CreditCardViewController())
        creditCardVC.tabBarItem = ESTabBarItem.init(TabBarBgContentView(),title:loan_home,image:UIImage.init(named: "creditcard_unselect"),selectedImage:UIImage.init(named: "creditcard_select"))
        creditCardVC.tabBarItem.tag = 1
        
        
        //贷款
//        let loanVC = KMNavigationController.init(rootViewController: LoanViewController())
//        loanVC.tabBarItem = ESTabBarItem.init(TabBarBgContentView(),title:loan_title,image:#imageLiteral(resourceName: "loan_unselect"),selectedImage:#imageLiteral(resourceName: "loan_select"))
        
        //资讯
        let informationVC = KMNavigationController.init(rootViewController: InformationRootViewController())
        informationVC.tabBarItem = ESTabBarItem.init(TabBarBgContentView(),title:information_title,image:UIImage.init(named: "information_unselect"),selectedImage:UIImage.init(named: "information_select"))
        informationVC.tabBarItem.tag = 3
        
        //我
        let mineVC = KMNavigationController.init(rootViewController: MineViewController())
        mineVC.tabBarItem = ESTabBarItem.init(TabBarBgContentView(),title:me_title,image:UIImage.init(named: "mine_unselect"),selectedImage:UIImage.init(named: "mine_select"))
        mineVC.tabBarItem.tag = 4
        
        self.tabBar.isTranslucent = false
        self.viewControllers = [creditCardVC,accountVC,informationVC,mineVC]

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

}

class TabBarBgContentView : ESTabBarItemContentView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        //调整每个item边距
        insets = UIEdgeInsets.init(top: 0, left: 0, bottom: 5, right: 0)

        //设置默认文字颜色
        textColor = UIColor.lightGray
        //设置选中文字颜色
        highlightTextColor = UIColor.tabBarColor()

        //设置默认文字颜色
        iconColor = UIColor.lightGray
        //设置选中图标颜色
        highlightIconColor = UIColor.tabBarColor()


    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 重写以下方法调整图片和文本大小位置
    override func updateLayout() {
        super.updateLayout()

        //调整图片大小和位置
        imageView.frame.size = CGSize.init(width: 20, height: 20)
        imageView.center.x = self.center.x

        //设置字体大小和位置
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 13))
        titleLabel.sizeToFit()
        titleLabel.center.x = self.center.x


    }
    
    
}

extension RootTabBarController : UITabBarControllerDelegate {
    
    //MARK: 监听选择按钮
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        //点击首页切换至信用卡
        if let nav = viewController as? KMNavigationController {
            if let vc = nav.rt_visibleViewController as? CreditCardViewController {
                vc.currentVC = 0
            }
        }
        
        return true
    }
    
}









